package com.bishe.bishe.Controller;

import com.bishe.bishe.Service.CommentService;
import com.bishe.bishe.Service.FeedbackService;
import com.bishe.bishe.entity.TextFilter;
import com.bishe.bishe.pojo.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private FeedbackService feedbackService;

    @RequestMapping("/findall")
    public PageInfo<Comments> findall(int page, int size){
        return commentService.findall(page,size);
    }

    @RequestMapping("/insert")
    public int add(@RequestBody Comments comments) {
        if (filter(comments.getCommentContent())==1)return 0;
        comments.setCommentContent(comments.getCommentContent());
        Orders orders=new Orders();
        orders.setOrderId(comments.getOrders().getOrderId());
        comments.setOrders(orders);
        comments.setCommentState("未回复");
        commentService.add(comments);
        return feedbackService.add(comments.getCommentId());
    }
    public int filter( String commentContent){
        TextFilter textFilter=new TextFilter();
        return textFilter.Filter(commentContent);
    }

    @RequestMapping("/findFeedback")
    public Feedback findFeedback(@Validated@RequestParam(name = "commentId") Integer commentId){
        return feedbackService.findByCommentId(commentId);
    }

    @RequestMapping("/update")
    public int update(@Validated@RequestParam(name = "commentId") Integer commentId,@RequestBody Feedback feedback){
        Feedback byCommentId = feedbackService.findByCommentId(commentId);
        Date date=new Date();
        feedback.setFeedbackTime(date);
        feedback.setFeedbackId(byCommentId.getFeedbackId());
        feedbackService.update(feedback);
        return commentService.update(byCommentId.getComments().getCommentId());
    }

    @RequestMapping("/findPage")/*分页+模糊查询，和上一个有区别，这个需要map数据*/
    public PageInfo <Comments> findPage(@RequestBody Map searchMap, int page, int size) {
        return commentService.findPage(searchMap, page, size);
    }
    @RequestMapping("/findByOrderId")
    public  List<Comments> findByOrderId(@Validated@RequestParam(name = "orderId",required = false) Integer orderId){
        return  commentService.findByOrderId(orderId);
    }
}
